package cn.diancan.action.manager;


import cn.diancan.model.types.CommonConstant;
import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.ModelDriven;

import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.model.Goods;
import cn.diancan.model.types.ApplySexType;
import cn.diancan.vo.GoodsUtil;
import cn.diancan.vo.manager.GoodsManagerActionVo;
import cn.diancan.vo.other.Pager;

/**
 * 货品
 * @author Administrator
 */
public class GoodsAction extends BaseAction implements ModelDriven<GoodsManagerActionVo> {

    private GoodsManagerActionVo requestVo = new GoodsManagerActionVo();

    private Pager pager = new Pager();

    //List
    @ManagerAuth
    public String goodsListPage() {
        try {
            pager.setPageNumber(requestVo.getPageNumber());
            pager.setPageSize(10);
            goodsService.findGoodsVoPager(pager);
            this.put("applySexTypeEnum", ApplySexType.values());
            this.put("pager", pager);
            this.put("requestVo", requestVo);
            return freemarkView("/views/ftl/manager/goods/goods_list.ftl");
        } catch (OutMsgException e) {
            return managerError(e.getMessage());
        }
    }

    @ManagerAuth
    public String createGoodsPage() {
        this.put("unitList", GoodsUtil.unitList);
        this.put("seasonList", GoodsUtil.seasonList);
        this.put("newGoodsNumber", goodsService.findNewGoodsNumber());
        this.put("applySexTypeEnum", ApplySexType.values());
        return freemarkView("/views/ftl/manager/goods/create_goods.ftl");
    }

    @ManagerAuth
    public String createGoods() {
        try {
            requestVo.setBrandId(CommonConstant.brandId);

            Goods goods2 = goodsService.saveGoods(requestVo.vo2GoodsModel(), true);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("goodsId", goods2.getId());
            jsonObject.put("goodsName", goods2.getGoodsName());

            return jsonFormat(jsonObject, "添加成功", "添加失败");
        }   catch (Exception e) {
            return jsonError(e);
        }
    }

    @ManagerAuth
    public String goodsInfo() {
        try {
            Goods goods = goodsService.findByGoodsId(requestVo.getGoodsId());
            this.put("unitList", GoodsUtil.unitList);
            this.put("seasonList", GoodsUtil.seasonList);
            this.put("applySexTypeEnum", ApplySexType.values());
            this.put("goods", goods);
            return freemarkView("/views/ftl/manager/goods/goods_info.ftl");
        } catch (OutMsgException e) {
            return managerError(e.getMessage());
        } catch (Exception e) {
            return managerError("系统繁忙");
        }
    }


    @ManagerAuth
    public String modifyGoods() {
        try {
            Boolean result = goodsService.updateGoods(requestVo.vo2GoodsModel());
            return jsonFormat(result, "修改成功", "修改失败");
        } catch (Exception e) {
            return jsonError(e);
        }
    }

  /*     
    @ManagerAuth
    public String removeGoods(){
        try {
            Boolean result = brandService.deleteBrandById(requestVo.getBrandId());
            return jsonFormat(result,"删除成功","删除失败");
        } catch (OutMsgException e) {
            return jsonError(e);
        }
    }*/

    @ManagerAuth
    public String moveGoods(){
        try {
            Boolean result = goodsService.updateMoveGoods(requestVo.getGoodsId(),requestVo.getMoveType());
            return jsonFormat(result,"操作成功","操作失败");
        } catch (Exception e) {
            return jsonError(e);
        }
    }


    @Override
    public GoodsManagerActionVo getModel() {
        return requestVo;
    }

}
